<?php
class orderAllControl extends skymvc{
	
	public function __construct(){		
		parent::__construct();
		$this->loadConfig("table");
		M("login")->checkLogin();
	}
	
	public function onDefault(){
		$where=" userid=".M("login")->userid;
		$status=get('status','i');
		$where.=" AND status=".$status;
		$option=array(
			"where"=>$where,
			"limit"=>3,
			"order"=>"order_id DESC"
		);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->goassign(array(
			"shoporder"=>$this->Shop($option),
			"ymorder"=>$this->ymdian($option),
			"kdorder"=>$this->koudai($option),
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
		));
		$tpl=M("pagetpl")->get("orderall","index");
		$this->smarty->display($tpl); 
		$this->smarty->display("orderall/index.html");	
	}
	
	public function Shop($option){
		$data=M("shop_order")->select($option);
		if($data){
			foreach($data as $v){
				$shopids[]=$v['shopid'];
			}
			$shops=M("shop")->getShopByIds($shopids);
			foreach($data as $k=>$v){
				$v['shop']=$shops[$v['shopid']];
				$num=$money=0;
				$order_products=M("shop_order_product")->select(array(
					"where"=>" order_id=".$v['order_id'],
				));
				if($order_products){
					foreach($order_products as $p){
						$num+=$p['amount'];
						$money+=$p['amount']*$p['price'];
					}
				}
				$v['num']=$num;
				$v['money']=$money;
				$data[$k]=$v;
			}
		}
		return $data;
	}
	
	public function ymdian($option){
		
		$data= M("ymdian_order")->select($option);
		if($data){
			foreach($data as $v){
				$shopids[]=$v['shopid'];
			}
			$shops=M("ymdian")->getShopByIds($shopids);
			foreach($data as $k=>$v){
				$v['shop']=$shops[$v['shopid']];
				$num=$money=0;
				$order_products=M("ymdian_order_product")->select(array(
					"where"=>" order_id=".$v['order_id'],
				));
				if($order_products){
					foreach($order_products as $p){
						$num+=$p['amount'];
						$money+=$p['amount']*$p['price'];
					}
				}
				$v['num']=$num;
				$v['money']=$money;
				$data[$k]=$v;
			}
		}
		return $data;
	}
	
	
	public function koudai($option){
		
		$data= M("koudai_order")->select($option);
		if($data){
			foreach($data as $v){
				$shopids[]=$v['shopid'];
			}
			$shops=M("koudai")->getShopByIds($shopids);
			foreach($data as $k=>$v){
				$v['shop']=$shops[$v['shopid']];
				$num=$money=0;
				$order_products=M("koudai_order_product")->select(array(
					"where"=>" order_id=".$v['order_id'],
				));
				if($order_products){
					foreach($order_products as $p){
						$num+=$p['amount'];
						$money+=$p['amount']*$p['price'];
					}
				}
				$v['num']=$num;
				$v['money']=$money;
				$data[$k]=$v;
			}
		}
		return $data;
	}
	
}

?>